package mspbots.autotask.data.core.companysiteconfigurations;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.autotask.common.ResultMsg;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Log4j2
@Component
@RequiredArgsConstructor
public class CompanySiteConfigurationsReceiver {

    private final CompanySiteConfigurationsRepository companySiteConfigurationsRepository;

//    @RabbitListener(queues = "auto-task.company-notes.sync")
    public void syncTask(ResultMsg content) {
        CompanySiteConfiguration companySiteConfiguration = CompanySiteConfiguration.builder().otherId(content.getId())
                .tenantId(content.getTenantId()).syncTime(content.getSyncDate())
                .entity(content.getEntity())
                .build();

        this.companySiteConfigurationsRepository.findByOtherIdAndTenantId(content.getId(), content.getTenantId())
                .map(ticket -> {
                    companySiteConfiguration.setId(ticket.getId());
                    return companySiteConfiguration;
                })
                .defaultIfEmpty(companySiteConfiguration)
                .flatMap(this.companySiteConfigurationsRepository::save)
                .block(Duration.ofSeconds(10));
    }
}
